package com.rimi.service.impl;

import com.rimi.bean.YqzcGoods;
import com.rimi.commons.CommonResult;
import com.rimi.commons.ResultStatus;
import com.rimi.mapper.ShoppingTrolleyMapper;
import com.rimi.model.dto.GoodsListDto;
import com.rimi.service.ShoppingTrolleyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author luka
 * @version V1.0
 * @date 2021 2021/7/5 0005 15:05
 */
@Service
public class ShoppingTrolleyServiceImpl implements ShoppingTrolleyService {

    @Autowired
    ShoppingTrolleyMapper shoppingTrolleyMapper;

    /**
     * 查询购物车商品
     * @param userId
     * @return
     */
    @Override
    public List<GoodsListDto> findAllGoodsInShoppingTrolleyByUserId(Integer userId) {

        List<YqzcGoods> allGoodsInShoppingTrolley = shoppingTrolleyMapper.findAllGoodsInShoppingTrolleyByUserId(userId);

        List<GoodsListDto> dtos = new ArrayList<>();

        for (YqzcGoods yqzcGoods : allGoodsInShoppingTrolley) {
            GoodsListDto goodsListDto = new GoodsListDto();
            BeanUtils.copyProperties(yqzcGoods,goodsListDto);
            dtos.add(goodsListDto);
        }
        return dtos;
    }


    /**
     * 根据用户id添加商品
     * @param userId
     * @param goodId
     * @param count
     * @return
     */
    @Override
    public CommonResult addGoodsToShoppingTrolley(Integer userId,Integer goodId, Integer count) {

        int trolleyId = shoppingTrolleyMapper.findShoppingTrolleyIdByUserId(userId);

        int res = shoppingTrolleyMapper.checkGoodIsExist(trolleyId,goodId);

        if (res == 0) {
            int row = shoppingTrolleyMapper.addGoodsToShoppingTrolley(trolleyId,goodId, count);

            return row == 1 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
        } else {
            int num = shoppingTrolleyMapper.getGoodCountInTrolley(trolleyId,goodId);

            int row = shoppingTrolleyMapper.updateGoodsInShoppingTrolleyByUserId(trolleyId,goodId,count + num);

            return row == 1 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.ADD_FAILED);
        }

    }

    /**
     * 删除
     * @param userId
     * @param goodId
     * @return
     */
    @Override
    public CommonResult deleteGoodsInShoppingTrolleyByUserId(Integer userId, Integer goodId) {

        int trolleyId = shoppingTrolleyMapper.findShoppingTrolleyIdByUserId(userId);

        int row = shoppingTrolleyMapper.deleteGoodsInShoppingTrolleyByUserId(trolleyId,goodId);

        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.DELETE_FAILED);
    }

    /**
     * 修改
     * @param userId
     * @param goodId
     * @param count
     * @return
     */
    @Override
    public CommonResult updateGoodsInShoppingTrolleyByUserId(Integer userId, Integer goodId, Integer count) {

        int trolleyId = shoppingTrolleyMapper.findShoppingTrolleyIdByUserId(userId);

        int row = shoppingTrolleyMapper.updateGoodsInShoppingTrolleyByUserId(trolleyId,goodId,count);

        return row != 0 ? CommonResult.onSuccess(null) : CommonResult.onFailed(ResultStatus.UPDATE_FAILED);
    }
}
